import 'dart:ui';

import 'package:cool_ui/cool_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_member/pages/course/index.dart';
import 'package:flutter_member/pages/index/index.dart';
import 'package:flutter_member/pages/personal_center/personal_center_screen.dart';
import 'package:flutter_member/pages/scholar/index.dart';
import 'package:flutter_member/pages/school_network/index.dart';
import 'package:flutter_member/widget/app_bar_actions.dart';
import 'package:flutter_member/widget/app_barleading.dart';
import 'package:flutter_member/widget/student_state.dart';
import 'package:flutter_screenutil/screenutil.dart';

/*
 * 底部切換標籤
 *
 */
class BottomNavigationWidget extends StatefulWidget {
  final int indexState;
  BottomNavigationWidget({Key key,this.indexState});
  @override
  _BottomNavigationWidgetState createState() => _BottomNavigationWidgetState();
}

class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  //現在的索引
  int _currentIndex = 0;
  //widget集合
  List<Widget> list = List();
  //重寫初始化方法,將需要導航的widget添加到集合
  @override
  void initState() {
    //修改默認索引
    _currentIndex = widget.indexState;
    list
      //..相當於list.
      ..add(IndexPage())
      ..add(IndexScholar())
      ..add(IndexCourse())
      ..add(IndexSchoolNetWork());
      //..add(PersonalCenterScreen());
    super.initState();
  }
  num color=0Xff4d80c5;
  num backcolor(){
      switch(_currentIndex){
        case 0:
          return 0Xff4d80c5;
          break;
        case 1:
          return 0Xff9eba31;
          break;
        case 2:
          return 0Xffffc128;
          break;
        case 3:
          return 0Xff154471;
          break;
      }

  }
  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    final height = MediaQuery.of(context).size.height;
    ScreenUtil.init(context, designSize: Size(375, 667), allowFontScaling: false);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(backcolor()),
        elevation: 0, //取消頂部陰影
        leading: AppBarLeading(), //個人中心
        actions: <Widget>[StudentStates()], //接送狀態
      ),
      body: list[_currentIndex],
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          borderRadius:BorderRadius.circular(20.0),
          color: Color(0Xffd1dfef),//邊框
        ),
        child: Container(
          margin: EdgeInsets.only(top: 5),
          child: ClipRRect(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(20),
                  topRight: Radius.circular(20)
              ),
              child: Container(
                decoration: BoxDecoration(
                  borderRadius:BorderRadius.circular(20.0),//邊框
                ),
                child: BottomNavigationBar(
                  //選中樣式
                  selectedLabelStyle:TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Color(0Xff154D7C)
                  ),
                  //未選中樣式
                  unselectedLabelStyle: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Color(0Xff7B7B7B)
                  ),
                  //超過3個不顯示名字，需要設置
                  type: BottomNavigationBarType.fixed,
                  items: [
                    getBottomNavigationBarItem(_currentIndex==0,_currentIndex==0?'images/index.png':'images/index_us.png','首頁'),
                    getBottomNavigationBarItem(_currentIndex==1,_currentIndex==1?'images/ielts.png':'images/ielts_us.png','補習'),
                    getBottomNavigationBarItem(_currentIndex==2,_currentIndex==2?'images/course.png':'images/course_us.png','課程'),
                    getBottomNavigationBarItem(_currentIndex==3,_currentIndex==3?'images/school_network.png':'images/school_network_us.png','分校網絡'),
                  ],
                  //高亮顯示
                  currentIndex: _currentIndex,
                  //點擊導航時更新現在的下標
                  onTap: (int index){
                    setState(() {
                      _currentIndex = index;
                    });
                  },
                ),
              )
          ),
        )
      ),
      drawer: Container(
          width: width * 0.7,
          height: height,
          color: Colors.transparent,
          child: ClipRect(
            child: BackdropFilter(
              filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
              child: Opacity(
                //设置透明度
                opacity: 0.8, //半透明
                child: PersonalCenterScreen(),
              ),
            ),
          )),
    );
  }

  Widget _buildPopoverButton(String btnTitle,String bodyMessage){
    return Padding(
        padding:  EdgeInsets.all(20.0),
        child:CupertinoPopoverButton(
            barrierColor: Color(0x01FFFFFF),
            child: Container(
              width: 80.0,
              height: 40.0,
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5.0)),
                  boxShadow: [BoxShadow(color: Colors.black12,blurRadius: 5.0)]
              ),
              child: Center(child:Text(btnTitle)),
            ),
            popoverBuild: (context) {
//              return Text("satatastas");
              return CupertinoPopoverMenuList(
                children: <Widget>[
                  CupertinoPopoverMenuItem(leading: Icon(Icons.add),child: Text("新增"),),
                  CupertinoPopoverMenuItem(leading: Icon(Icons.edit),child: Text("修改"),),
                  CupertinoPopoverMenuItem(leading: Icon(Icons.delete),child: Text("删除"),)
                ],
              );
            })

    );

  }
}

BottomNavigationBarItem getBottomNavigationBarItem(bool bl,String iconSrc,String title){
  return BottomNavigationBarItem(
    icon: Image.asset(iconSrc,width: 28,height: 28,),
    //icon: Icon(Icons.clear),
    title: Container(
      margin: EdgeInsets.only(top: 3),
      child: Text(
        title,
        style: TextStyle(
          color: bl?Color(0xff154D7C):Color(0xff7B7B7B)
        ),
      ),
    ),
  );
}
